package com.wlb.es.service.impl;

import com.wlb.es.entity.User;
import com.wlb.es.mapper.UserRepository;
import com.wlb.es.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserRepository userRepository;

    /**
     * 添加用户
     */
    @Override
    public void add() {
        userRepository.save( new User("4","lisi",19));
    }

    /**
     * 查询一个user
     */
    public List<User> findById(String id){
        User user = userRepository.findById(id).get();
        ArrayList<User> list = new ArrayList<>();
        list.add(user);
        return list;
    }

    /**
     * 查询所有用户
     * @return
     */
    @Override
    public List<User> findAll() {
        Iterable<User> all = userRepository.findAll();
        ArrayList<User> list = new ArrayList<>();
        for (User userinfo : all) {
            list.add(userinfo);
            System.out.println(userinfo);
        }
        return list;
    }

    /**
     * 删除用户
     */
    @Override
    public void deleteById(){
        userRepository.deleteById("4");
    }

    /**
     * 修改用户
     */
    public void update(){
        User user = userRepository.findById("4").get();
        user.setName("lisi1");
        user.setAge(22);
        userRepository.save(user);
    }

}
